草庐IT

第二节: 模块化Module

全部标签

javascript - 如何让 Karma + Webpack 找到模块?

在webpack通过Karma测试运行器将它们组合在一起后,我想在一堆模块上运行我的测试,但是每当我运行我的测试时,Karma都会说,"Error:Cannotfindmodule"hello.js"athttp://localhost:9877/base/src/hello.spec.js?d301966ffc1330826574d9d8fff5a644c3390c68:47"我有一个规范文件:vara=require('hello.js');describe("atesttest",function(){it("humperdinktest",function(){expect(a

javascript - 不提供文件扩展名就无法导入 TypeScript 模块

我是TypeScript的新手,我希望我应该能够导入我的TS文件而无需指定它们是TS文件。我必须做import{sealed}from"./decorators/decorators.ts";而不是我想要的是正确的方式这是import{sealed}from"./decorators/decorators";这会导致错误表明它只查找以.js或.jsx结尾的文件我的tsconfig.json看起来像这样{"compileOnSave":true,"compilerOptions":{"module":"commonjs","moduleResolution":"node","jsx":"

javascript - 错误 : Cannot find module 'wrench' , 当我运行 gulp 命令时出现此错误

我已经在本地和全局安装了npm、bower和gulp。当我在该文件夹中运行gulp时,仍然出现此错误。Error:Cannotfindmodule'wrench'atFunction.Module._resolveFilename(module.js:325:15)atFunction.Module._load(module.js:276:25)atModule.require(module.js:353:17)atrequire(internal/module.js:12:17)atObject.(/home/myPC/documents/workspace/frontend/gul

javascript - 从模块中调用 Node.js 模块函数

我正在尝试编写一个Node模块以清理我的代码并将其分离到不同的文件中。考虑下面的代码:module.exports={Hello:function(request,reply){returnreply("Hello"+World());},World:function(){return"World";}}如果我导入上述模块并使用Hello函数作为特定路由的处理程序,我会收到HTTP500内部服务器错误。如果我将Hello函数更改为,我已将问题缩小到对World()的调用Hello:function(request,reply){returnreply("HelloWorld");}然后

javascript - 如何用 Jest 模拟第三方模块

我的测试目标中有当前导入:importsharpfrom'sharp'并在我的同一个测试目标中使用它:returnsharp(local_read_file).raw().toBuffer().then(outputBuffer=>{在我的测试中,我正在执行以下操作来模拟sharp函数:jest.mock('sharp',()=>{raw:jest.fn()toBuffer:jest.fn()then:jest.fn()})但我得到:return(0,_sharp2.default)(local_read_file).^TypeError:(0,_sharp2.default)isno

Javascript 多级 JSON 对象数组 - 如何访问第二级或更高级别的键值对

考虑以下JSON对象数组:myList=[{title:"Parent1",children:[{childname:"Child11"},{childname:"Child12"}],cars:[{carname:"Car11"},{carname:"Car12"}]},{title:"Parent2",children:[{childname:"Child21"},{childname:"Child22"}],cars:[{carname:"Car21"},{carname:"Car22"}]}];如何在javascript中访问“Child21”?以下选项无效:varmyStri

javascript - 单元测试 AngularJS 模块 Controller

我正在查看TODOMVCAngularJS示例,我看到该应用程序被定义为一个模块。vartodomvc=angular.module('todomvc',[]);在Controller内部,我看到它们被定义为:todomvc.controller('TodoCtrl',functionTodoCtrl($scope,$location,todoStorage,filterFilter){//...});我的问题涉及单元测试...如何为该类编写单元测试?我试过这样的事情:describe('TodoCtrl',function(){varcontroller;beforeEach(fun

javascript - 如何知道 npm `unzip` 模块何时完成解压缩文件?

我正在使用npm的unzip模块来提取zip存档的内容。我需要知道它何时完成提取以及文件已完全写入磁盘。我的代码:fs.createReadStream('master.zip').pipe(unzip.Extract({path:'gitdownloads/repo'}));我尝试过的:我的第一个想法是我可以接入流并监听完成事件,但是unzip只接受输入:它不会返回另一个流。我还查看了unzip模块是否有“完成”回调。运气不好。 最佳答案 来自githubREADMEExtractemitsthe'close'eventoncet

javascript - 如何在 Node js 中导出带有 module.exports 的数组?

我有一个使用node.js的项目。这是我第一次使用nodejs,我想将一个数组导出到我的应用程序。这是一些代码:module.exports={vararrays=[];arrays[0]='array0';arrays[1]='array1';arrays[2]='array2';arrays[3]='array3';arrays[4]='array4';varr_array=arrays[Math.floor(Math.random()*arrays.length)].toString();}最后我想在我的app.js中使用varr_array但我不知道如何使用。

javascript - 如何使用 Javascript ES6 ES2015 模块将常量直接导出/导入到导入模块命名空间?

好吧,我不太能找到这个问题的答案。我正在使用webpack和babeles2015preset来处理ES2015模块。要导出的模块1,文件名“foobar.js”exportconstFOO='foo'exportconstBAR='bar'有没有办法将这个常量导入我的导入模块中的全局命名空间?我想在将使用常量的模块中执行此操作:import'foobar'constdoSomething=()=>{console.log(FOO+BAR)}我知道这行得通:import*asCONSTANTSfrom'foobar'constdoSomething=()=>{console.log(C